import { NextFunction, Request, Response } from "express";
import UserService from "../services/UserService";
import ResponseDTO from "@/dto/ResponseDTO";
class UserController {
  // 注册
  public async createUser(req: Request, res: Response, next: NextFunction) {
    try {
      console.log(">>>>>>>>>>>>>>>>>>>>>>>>");
      const userParams = req.body;
      await UserService.createUser(userParams);
      res.locals.data = true;
      next();
    } catch (error) {
      res.locals.error = error;
      next();
    }
  }

  /** 获取用户列表 */
  public async getUserList(req: Request, res: Response, next: NextFunction) {
    try {
      const listParams = {
        pageSize: Number(req.query?.pageSize || 10),
        pageIndex: Number(req.query?.pageIndex || 1),
        userName: String(req.query?.userName || ""),
        email: String(req.query?.email || ""),
      };
      const userList = await UserService.getUserList(listParams);
      res.locals.data = userList;
      next();
    } catch (error) {
      res.locals.error = error;
      next();
    }
  }

  /** 更新用户信息 */
  public async updateUser(req: Request, res: Response, next: NextFunction) {
    try {
      const userParams = req.body;
      if (!userParams?.userId) {
        res.send(ResponseDTO.error(100, "参数错误，userId不能为空"));
        return;
      }
      await UserService.updateUser(userParams);
      res.locals.data = true;
      next();
    } catch (error) {
      res.locals.error = error;
      next();
    }
  }

  /** 删除用户信息 */

  public async deleteUser(req: Request, res: Response, next: NextFunction) {
    try {
      const userParams = req.body;
      if (!userParams?.userId) {
        res.send(ResponseDTO.error(100, "参数错误，userId不能为空"));
        return
      }
      await UserService.deleteUser(userParams.userId as string);
      res.locals.data = true;
      next();
    } catch (error) {
      res.locals.error = error;
      next();
    }
  }
  // 获取用户信息
  public async getUserById(req: Request, res: Response, next: NextFunction) {
    try {
      const userId = (req.query?.userId as string) || "";
      const user = await UserService.getUserById(userId);
      res.locals.data = user;
      next();
    } catch (error) {
      res.locals.error = error;
      next();
    }
  }
  // 登录
  // public async userLogin(req: Request, res: Response, next: NextFunction) {
  //   try {
  //     const userParams: { username: string; password: string } = req.body;
  //     const loginResult = await UserService.userLogin(userParams);
  //     switch (loginResult.code) {
  //       case 200: {
  //         res.locals.data = loginResult.token;
  //         break;
  //       }
  //       case 401: {
  //         res.locals.error = loginResult;
  //         break;
  //       }
  //       default: {
  //         res.locals.error = "内部错误，登录失败";
  //         /** empty */
  //       }
  //     }
  //     next();
  //   } catch (error) {
  //     res.locals.error = error;
  //     next();
  //   }
  // }
}

export default new UserController();
